// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package fr.upstlse.glre.business.service.communiquer;

import java.util.Collection;
import java.util.HashSet;

import org.apache.log4j.Logger;

import fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettres;
import fr.upstlse.glre.business.domain.communiquer.BoiteAuxLettresDao;
import fr.upstlse.glre.business.domain.user.User;
import fr.upstlse.glre.business.vo.communiquer.BoiteAuxLettresVO;
import fr.upstlse.glre.business.vo.communiquer.MessageVO;

/**
 * @see fr.upstlse.glre.business.service.communiquer.BoiteAuxLettresService
 */
public class BoiteAuxLettresServiceImpl
    extends fr.upstlse.glre.business.service.communiquer.BoiteAuxLettresServiceBase
{

	private static final Logger LOGGER = Logger.getLogger(BoiteAuxLettresServiceImpl.class);
	
    /**
     * @see fr.upstlse.glre.business.service.communiquer.BoiteAuxLettresService#findByProprietaire(fr.upstlse.glre.business.vo.user.UserVO)
     */
    protected fr.upstlse.glre.business.vo.communiquer.BoiteAuxLettresVO handleFindByProprietaire(fr.upstlse.glre.business.vo.user.UserVO pProprietaireVO)
        throws java.lang.Exception
    {
        User lProprietaire = getUserDao().userVOToEntity(pProprietaireVO);
        BoiteAuxLettresVO lBoiteAuxLettresVO = (BoiteAuxLettresVO) getBoiteAuxLettresDao().findByProprietaire(BoiteAuxLettresDao.TRANSFORM_BOITEAUXLETTRESVO, lProprietaire);
        return lBoiteAuxLettresVO;
    }

    /**
     * @see fr.upstlse.glre.business.service.communiquer.BoiteAuxLettresService#findByProprietaireId(long)
     */
    protected fr.upstlse.glre.business.vo.communiquer.BoiteAuxLettresVO handleFindByProprietaireId(long pProprietaireId)
        throws java.lang.Exception
    {
        //Get user
    	User lProprietaire = getUserDao().load(pProprietaireId);
    	BoiteAuxLettresVO lBoiteAuxLettresVO = (BoiteAuxLettresVO) getBoiteAuxLettresDao().findByProprietaire(BoiteAuxLettresDao.TRANSFORM_BOITEAUXLETTRESVO, lProprietaire);
        return lBoiteAuxLettresVO;
    }

	@Override
	protected void handleRecevoir(BoiteAuxLettresVO pBoiteAuxLettresVO,
			MessageVO pMessageVO) throws Exception {
		Collection<MessageVO> lMessageVOs = pBoiteAuxLettresVO.getMessages();
		if(lMessageVOs == null) {
			lMessageVOs = new HashSet<MessageVO>();
		}
		if(LOGGER.isDebugEnabled()) {
    		LOGGER.debug("Add message");
    	}
		lMessageVOs.add(pMessageVO);
		pBoiteAuxLettresVO.setMessages(lMessageVOs);
		
		BoiteAuxLettres lBoiteAuxLettres = getBoiteAuxLettresDao().boiteAuxLettresVOToEntity(pBoiteAuxLettresVO);
		getBoiteAuxLettresDao().update(lBoiteAuxLettres);
	}

	@Override
	protected void handleCreate(
			BoiteAuxLettresVO pBoiteAuxLettresVO) throws Exception {
		BoiteAuxLettres lBoiteAuxLettres = getBoiteAuxLettresDao().findByProprietaire(getUserDao().userVOToEntity(pBoiteAuxLettresVO.getProprietaire()));
		if(lBoiteAuxLettres == null) {
			lBoiteAuxLettres = getBoiteAuxLettresDao().boiteAuxLettresVOToEntity(pBoiteAuxLettresVO);
			getBoiteAuxLettresDao().create(lBoiteAuxLettres);
		}
	}

	@Override
	protected BoiteAuxLettresVO handleFindByProprietaireLogin(
			String pProprietaireLogin) throws Exception {
		//Get user
    	User lProprietaire = getUserDao().findByLogin(pProprietaireLogin);
    	BoiteAuxLettresVO lBoiteAuxLettresVO = (BoiteAuxLettresVO) getBoiteAuxLettresDao().findByProprietaire(BoiteAuxLettresDao.TRANSFORM_BOITEAUXLETTRESVO, lProprietaire);
        return lBoiteAuxLettresVO;
	}

}